<?php
/**
 * ExecutionTime.php
 * 
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.2
 */

/** base class */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Naneau_Controller_Plugin_ExecutionTime
 * 
 * a simple plugin to add the current execution time to html pages
 *
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Plugin_ExecutionTime extends Zend_Controller_Plugin_Abstract {
    
    /**
     * dispatchLoopShutdown hook
     * 
     * runs after all controller actions have finished. appends execution time
     * 
     * @return void
     */
    public function dispatchLoopShutdown() {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
        //view renderer action helper
        if (!$viewRenderer->getNeverRender()) {
            //viewRenderer is set to render (assuming HTML output)

            $front = Zend_Controller_Front::getInstance();
            //the front controller
            if ($startTime = $front->getParam('timeStart')) {
                //the parameter was set (yay!)
                $totalTime = microtime(true) - $startTime;
                //calculate the time difference
                $this->getResponse()->appendBody("\n<!-- total execution time: $totalTime . //-->");
                //append a comment to the HTML body
            }
        }
    }
}